#include "Line.h"
void MidBresenham(Point p0, Point p1)
{
	// 交换点
	if (p0.x > p1.x)
	{
		Point t = p0;
		p0 = p1;
		p1 = t;
	}
	GLint deltaX = p1.x - p0.x;
	GLint deltaY = p1.y - p0.y;
	GLint x = p0.x, y = p0.x;
	double k = (deltaY + 0.0) / deltaX;
	GLint d;

	if (k > 1)
	{
		d = deltaY - 2 * deltaX;
		while (y <= p1.y)
		{

			putPixel(x, y);
			if (d < 0)
			{
				d += 2 * (deltaY - deltaX);
				x += 1;
			}
			else
			{
				d -= 2 * deltaX;
			}
			y += 1;
		}
	}
	else
	{
		d = deltaX - 2 * deltaY;

		while (x <= p1.x)
		{

			putPixel(x, y);

			if (d < 0)
			{
				d += 2 * (deltaX - deltaY);
				y += 1;
			}
			else
			{
				d -= 2 * deltaY;
			}
			x += 1;
		}
	}
}

void Bresenham(Point p0, Point p1)
{
	// 交换点
	if (p0.x > p1.x)
	{
		Point t = p0;
		p0 = p1;
		p1 = t;
	}
	GLint deltaX = p1.x - p0.x;
	GLint deltaY = p1.y - p0.y;
	double k = (deltaY + 0.0) / deltaX;
	GLint x = p0.x, y = p0.x;
	GLint d, e = -deltaX;

	if (k > 1)
	{
		while (y < p1.y)
		{
			// 绘制点
			putPixel(x, y);
			y++;
			e += 2 * deltaX;
			if (e > 0)
			{
				x++;
				e -= 2 * deltaY;
			}
		}
	}
	else
	{
		while (x < p1.x)
		{
			// 绘制点
			putPixel(x, y);
			x++;
			e += 2 * deltaY;
			if (e > 0)
			{
				y++;
				e -= 2 * deltaX;
			}
		}
	}
}